#include "bsv_param.h"

#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QGroupBox>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLabel>
#include <QtWidgets/QDateEdit>
#include <QtWidgets/QTimeEdit>
#include <QtWidgets/QDateTimeEdit>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QRadioButton>
#include <QtWidgets/QScrollArea>
#include <QtWidgets/QWidget>
#include <QtWidgets/QDoubleSpinBox>
#include <QDateTime>
#include <QDir>
#include <QSettings>


int _WPPS_(
    const QString& strAppName,
    const QString& strKeyName,
    const QString& strString,
    const QString& strFileName)
{
    QSettings *configIni = new QSettings(strFileName, QSettings::IniFormat);

    configIni->beginGroup(strAppName);
    configIni->setValue(strKeyName,strString);
    configIni->endGroup();

    delete  configIni;

    return 0;
}

int _GPPS_(
    const QString& strAppName,
    const QString& strKeyName,
    const QString& lpDefault,
    QString& strReturnedString,
    const QString& strFileName)
{
    QSettings *configIni = new QSettings(strFileName, QSettings::IniFormat);

    configIni->beginGroup(strAppName);
    strReturnedString=configIni->value(strKeyName, lpDefault).toString();
    configIni->endGroup();

    delete  configIni;

    return strReturnedString.length();
}

void write_set(const std::vector<int>& value, QString key, QString set_file, QString section)
{
	if (set_file == "") return;
    QString str_file = "./config/" + set_file + ".ini";

	QString vlst;
	for (auto& a : value)
		vlst = vlst + QString::number(a) + ",";
	if (vlst.right(1) == ",")
		vlst = vlst.left(vlst.length() - 1);

    _WPPS_(section, key, vlst, str_file);
}

void write_set(const std::vector<double>& value, QString key, QString set_file, QString section)
{
	if (set_file == "") return;
    QString str_file = "./config/" + set_file + ".ini";

	QString vlst;
	for (auto& a : value)
		vlst = vlst + QString::number(a) + ",";
	if (vlst.right(1) == ",")
		vlst = vlst.left(vlst.length() - 1);

    _WPPS_(section, key, vlst, str_file);
}

void write_set(int value, QString key, QString set_file, QString section)
{
	if (set_file == "") return;
    QString str_file = "./config/" + set_file + ".ini";
    _WPPS_(section, key, QString::number(value), str_file);
}

void write_set(double value, QString key, QString set_file, QString section)
{
	if (set_file == "") return;
    QString str_file = "./config/" + set_file + ".ini";
    _WPPS_(section, key, QString::number(value), str_file);
}


void write_set(QString value, QString key, QString set_file, QString section)
{
	if (set_file == "") return;
    QString str_file = "./config/" + set_file + ".ini";
    _WPPS_(section, key, value, str_file);
}

void get_set(std::vector<int>& value, QString key, QString set_file, QString defaval, QString section)
{
    QString str_file = "./config/" + set_file + ".ini";
    QString values;
    _GPPS_(section, key, defaval, values, str_file);
    QStringList vlst = values.split(",");
	for (auto& a : vlst)
		value.push_back(a.toInt());
}

void get_set(std::vector<double>& value, QString key, QString set_file, QString defaval, QString section)
{
    QString str_file = "./config/" + set_file + ".ini";
    QString values;
    _GPPS_(section, key, defaval, values, str_file);
    QStringList vlst = values.split(",");
	for (auto& a : vlst)
		value.push_back(a.toDouble());
}


void get_set(int& value, QString key, QString set_file, int defaval, QString section)
{
    QString def = QString::number(defaval);
    QString str_file = "./config/" + set_file + ".ini";
    QString strVal;
    _GPPS_(section, key, def, strVal, str_file);
    value=strVal.toInt();
}

void get_set(double& value, QString key, QString set_file, double defaval, QString section)
{
    QString def = QString::number(defaval);
    QString str_file = "./config/" + set_file + ".ini";
    QString strVal;
    _GPPS_(section, key, def, strVal, str_file);
    value=strVal.toInt();
}


void get_set(QString& value, QString key, QString set_file, QString defaval, QString section)
{
    QByteArray ba = defaval.toLocal8Bit();
    QString str_file = "./config/" + set_file + ".ini";
    _GPPS_(section, key, ba.data(), value, str_file);
}



QString get_p_v(QString camName, const QStringList& paramList, QString key)
{
    for (auto v : paramList)
    {
        int idx = v.indexOf("=");
        QString key_v = v.left(idx + 0);
        QString value = v.mid(idx + 1);

        if (key == key_v)
        {
            return value;
        }
    }
    return "";
}



QString get_p_v(const QStringList& paramList, QString key)
{
    for (auto v : paramList)
    {
        int idx = v.indexOf("=");
        QString key_v = v.left(idx + 0);
        QString value = v.mid(idx + 1);

        if (key == key_v)
        {
            return value;
        }
    }
    return "";
}

bsv_param::bsv_param(QObject * parent) : QObject(parent) {

}

bsv_param::~bsv_param() {

}

void bsv_param::load(const QObject * myObject, QString strIniFile)
{
	QString class_name = QString::fromLocal8Bit(myObject->metaObject()->className());
	QString obj_name = myObject->objectName();

	if (class_name == "QCheckBox")
	{
		QCheckBox* chkBox = (QCheckBox*)myObject;
		int v;
		get_set(v, obj_name, strIniFile);
		chkBox->setChecked(v);
	}

	if (class_name == "QLineEdit")
	{
		QLineEdit* lnEdit = (QLineEdit*)myObject;
		QString v;
		get_set(v, obj_name, strIniFile);
		lnEdit->setText(v);
	}

	if (class_name == "QTimeEdit")
	{
		QTimeEdit* pObj = (QTimeEdit*)myObject;

		QString v;
		get_set(v, obj_name, strIniFile);
		QTime time = QTime::fromString(v, "hh:mm:ss");
		//设置日期
		pObj->setTime(time);
	}

	if (class_name == "QDateEdit")
	{
		QDateEdit* pObj = (QDateEdit*)myObject;
		QString v;
		get_set(v, obj_name, strIniFile);
		QDate date = QDate::fromString(v, "yyyy-MM-dd");
		pObj->setDate(date);
	}

	if (class_name == "QDateTimeEdit")
	{
		QDateTimeEdit* pObj = (QDateTimeEdit*)myObject;
		QString v;
		get_set(v, obj_name, strIniFile);
		QDateTime datetime = QDateTime::fromString(v, "yyyy-MM-dd hh:mm:ss");
		pObj->setDateTime(datetime);
	}

	if (class_name == "QRadioButton")
	{
		QRadioButton * pObj = (QRadioButton*)myObject;
		int v;
		get_set(v, obj_name, strIniFile);
		pObj->setChecked(v);
	}

	if (class_name == "QDoubleSpinBox")
	{
		QDoubleSpinBox * pObj = (QDoubleSpinBox*)myObject;
		double v;
		get_set(v, obj_name, strIniFile);
		pObj->setValue(v);
	}
	if (class_name == "QSpinBox")
	{
		QSpinBox * pObj = (QSpinBox*)myObject;
		int v;
		get_set(v, obj_name, strIniFile);
		pObj->setValue(v);
	}

	//遍历子控件
	const QObjectList groupbox_list = myObject->children();

	foreach(QObject* groupbox, groupbox_list)
	{
		load(groupbox, strIniFile);
	}
}

//写入参数档
void bsv_param::write(const QObject * myObject, QString strIniFile)
{

	QString class_name = QString::fromLocal8Bit(myObject->metaObject()->className());
	QString obj_name = myObject->objectName();

	if (class_name == "QCheckBox")
	{
		QCheckBox* chkBox = (QCheckBox*)myObject;
		int v = chkBox->isChecked();
		write_set(v, obj_name, strIniFile);
	}

	if (class_name == "QLineEdit")
	{
		QLineEdit* lnEdit = (QLineEdit*)myObject;

		QString v = lnEdit->text();
		write_set(v, obj_name, strIniFile);
	}

	if (class_name == "QTimeEdit")
	{
		QTimeEdit* pObj = (QTimeEdit*)myObject;
		QString v = pObj->time().toString("hh:mm:ss");
		write_set(v, obj_name, strIniFile);
	}

	if (class_name == "QDateEdit")
	{
		QDateEdit* pObj = (QDateEdit*)myObject;
		QString v = pObj->date().toString("yyyy-MM-dd");
		write_set(v, obj_name, strIniFile);
	}

	if (class_name == "QDateTimeEdit")
	{
		QDateTimeEdit* pObj = (QDateTimeEdit*)myObject;
		QString v = pObj->dateTime().toString("yyyy-MM-dd hh:mm:ss");
		write_set(v, obj_name, strIniFile);
	}

	if (class_name == "QRadioButton")
	{
		QRadioButton * pObj = (QRadioButton*)myObject;
		int v = pObj->isChecked();
		write_set(v, obj_name, strIniFile);
	}
	if (class_name == "QDoubleSpinBox")
	{
		QDoubleSpinBox * pObj = (QDoubleSpinBox*)myObject;
		double v = pObj->value();
		write_set(v, obj_name, strIniFile);
	}
	if (class_name == "QSpinBox")
	{
		QSpinBox * pObj = (QSpinBox*)myObject;
		int v = pObj->value();
		write_set(v, obj_name, strIniFile);
	}

	//遍历子窗口
	const QObjectList groupbox_list = myObject->children();

	foreach(QObject* groupbox, groupbox_list)
	{
		write(groupbox, strIniFile);
	}

}
